<?php
require_once('categorie.php');
require_once('label.php');

class Label extends PDO {
	const TABLE = 'Categorie';
	const TABLE_TYPE = 'CategorieType';
	const TABLE_LABEL = 'Label';
	const TABLE_RELATION_CONCEPT = 'Concept_has_Categorie';

	const ID = 'idCategorie';
	const ID_TYPE = 'idCategorieType';
	const ID_CONCEPT = 'idConcept';
	const DESCRIPTION = 'description';
	const LABEL = 'label';
	const LANGUE = 'idLangue';
	const ID_TABLE = 'idInTable';
	const NOM_TABLE = 'nomTable';

	private $connection;
	private $stmt_select_by_id;
	private $stmt_select_by_concept;

	public function __construct($connection) {
		$this->connection = $connection;
		$this->prepare_statements();
	}

	public static function getTableName() {
		return TABLE;
	}

	public function getBeanById($id, $langue) {
		$this->stmt_select_by_id->bindParam(':id', $id, PDO::PARAM_INT);
		$this->stmt_select_by_id->bindParam(':lang', $langue, PDO::PARAM_STR);
		$this->stmt_select_by_id->bindParam(':table', TABLE_TYPE, PDO::PARAM_INT);
		$this->stmt_select_by_id->execute();
		return $this->stmt_select_by_id->fetchObject('CategorieBean');
	}

	public function getListByConcept($id, $langue) {
		$this->stmt_select_by_concept->bindParam(':id', $id, PDO::PARAM_INT);
		$this->stmt_select_by_concept->execute();
		return $this->stmt_select_by_name->fetchAll(PDO::FETCH_CLASS, 'CategorieBean');
	}
	private function prepare_statements() {
		$sql = 'SELECT '.self::ID.', '.self::ID_TYPE.', '.self::LABEL.
			' FROM '.self::TABLE.', '.self::TABLE_TYPE.', '.self::TABLE_LABEL.
			' WHERE '.self::TABLE.'.'.self::ID.' = :id AND ('.
				self::TABLE.'.'.self::ID_TYPE.' = '.self::TABLE_TYPE.'.'.self::ID.') AND ('.
				self::TABLE_LABEL.'.'.self::NOM_TABLE.' = :table) AND ('.
				self::TABLE_LABEL.'.'.self::ID_TABLE.' = '.self::TABLE_TYPE.'.'.self::ID.') AND ('.
				self::TABLE_LABEL.'.'.self::LANGUE.' = :lang)';
		$this->stmt_select_by_id = $this->connection->prepare($sql);

		$sql = 'SELECT '.self::ID.', '.self::ID_TYPE.', '.self::LABEL.
			' FROM '.self::TABLE.', '.self::TABLE_TYPE.', '.self::TABLE_LABEL.', '.self::TABLE_RELATION_CONCEPT.
			' WHERE '.self::TABLE_RELATION_CONCEPT.'.'.self::ID_CONCEPT.' = :id AND ('.
				self::TABLE_RELATION_CONCEPT.'.'.self::ID.' = '.self::TABLE.'.'.self::ID.') AND ('.
				self::TABLE.'.'.self::ID_TYPE.' = '.self::TABLE_TYPE.'.'.self::ID.') AND ('.
				self::TABLE_LABEL.'.'.self::NOM_TABLE.' = :table) AND ('.
				self::TABLE_LABEL.'.'.self::ID_TABLE.' = '.self::TABLE_TYPE.'.'.self::ID.') AND ('.
				self::TABLE_LABEL.'.'.self::LANGUE.' = :lang)';
		$this->stmt_select_by_concept = $this->connection->prepare($sql);
	}
}
?>
